using zhwy.saas.api.Entity.Sys;
using zhwy.saas.api.Extensions.Log;
using zhwy.saas.api.Repository;
using zhwy.saas.api.Services.Sinterface.Sys;

namespace zhwy.saas.api.Services.Sys
{
    /// <summary>
    /// 角色权限关联服务实现
    /// </summary>
    public class SysRolePermissionService : BaseService<Roles_permissionsEntity>, ISysRolePermissionService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">角色权限关联仓储接口</param>
        /// <param name="logger">日志服务</param>
        public SysRolePermissionService(IBaseRepository<Roles_permissionsEntity> repository, ILogExt logger)
            : base(repository, logger)
        {
        }

        /// <summary>
        /// 批量分配角色权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="permissionIds">权限ID列表</param>
        /// <returns>是否分配成功</returns>
        public async Task<bool> AssignPermissionsAsync(int roleId, List<int> permissionIds)
        {
            try
            {
                // 先删除原有的权限
                await RemoveRolePermissionsAsync(roleId);

                // 添加新的权限
                var rolePermissions = permissionIds.Select(pid => new Roles_permissionsEntity
                {
                    Role_id = roleId,
                    Permissions_id = pid,
                }).ToList();

                foreach (var rp in rolePermissions)
                {
                    await _repository.AddAsync(rp);
                }

                return true;
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"分配角色[{roleId}]权限失败");
                return false;
            }
        }

        /// <summary>
        /// 移除角色的所有权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>是否移除成功</returns>
        public async Task<bool> RemoveRolePermissionsAsync(int roleId)
        {
            try
            {
                // 删除角色的所有权限
                var existingPermissions = await _repository.GetListAsync(rp => rp.Role_id == roleId);
                foreach (var permission in existingPermissions)
                {
                    await _repository.DeleteAsync(permission);
                }
                return true;
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"移除角色[{roleId}]权限失败");
                return false;
            }
        }

        /// <summary>
        /// 获取角色的权限ID列表
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>权限ID列表</returns>
        public async Task<List<int>> GetPermissionIdsByRoleIdAsync(int roleId)
        {
            var rolePermissions = await _repository.GetListAsync(rp => rp.Role_id == roleId);
            return rolePermissions.Select(rp => rp.Permissions_id).ToList();
        }

        /// <summary>
        /// 检查角色是否拥有指定权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="permissionId">权限ID</param>
        /// <returns>是否拥有权限</returns>
        public async Task<bool> HasPermissionAsync(int roleId, int permissionId)
        {
            var rolePermission = await _repository.GetListAsync(rp =>
                rp.Role_id == roleId && rp.Permissions_id == permissionId);
            return rolePermission != null;
        }
    }
}